<template>
  <div class="edit-wrap">
    <div class="edit-box">
      <img :src="workInfo.mechanismLogo" class="pic1" onclick="return false"/>
      <div class="company-box">
        <span class="title1">招聘岗位：{{workInfo.workTypeName}}</span>
        <span class="title2">工种：{{workInfo.postType == 0?"正式工":"小时工"}}</span>
        <span class="title3">{{workInfo.mechanismName}}</span>
      </div>
    </div>

    <div class="content-box">
      <span class="title4">招聘人数:</span>
      <div class="edit-con">
        <input type="text" class="edit" v-model="maxNumber" placeholder="请输入招聘人数" maxlength="5"
               oninput="this.value=this.value.replace(/\D/g,'').replace(/^0+(?=\d)/,'')"/>
      </div>
    </div>

    <div class="content-box">
      <span class="title4">性别年龄限制:</span>
      <div class="edit-con">
        <input type="text" class="edit" v-model="age" placeholder="例：女18~35岁 男18~40岁" maxlength="15"/>
      </div>
    </div>

    <div class="content-box">
      <span class="title4">面试开始时间:</span>
      <datetime format="HH:mm" v-model="startTime" class="num" placeholder="请选择开始时间"></datetime>
      <img src="./in.png" class="pic-in" onclick="return false"/>
    </div>
    <div class="content-box">
      <span class="title4">面试结束时间:</span>
      <datetime format="HH:mm" v-model="endTime" class="num" placeholder="请选择结束时间"></datetime>
      <img src="./in.png" class="pic-in" onclick="return false"/>
    </div>
    <div class="content-box">
      <span class="title4">招聘状态:</span>
      <popup-picker :data="statusList" v-model="status" class="num"
                    :class="{'active1':workInfo.status == 0,'active2':workInfo.status == 1}"
                    placeholder="请选择招聘状态"></popup-picker>
      <img src="./in.png" class="pic-in" onclick="return false"/>
    </div>
    <div class="content-box">
      <span class="title4">招聘要求:</span>
      <span class="num" @click="demandEdit">修改</span>
      <img src="./in.png" class="pic-in" onclick="return false"/>
    </div>
    <div class="content-box">
      <span class="title4">是否发送系统消息:</span>
      <van-radio-group v-model="sendMsg" class="radio-box">
        <van-radio name="1" checked-color="#3cafff" @click="sendMsg = '1'" class="radio-send">不发送</van-radio>
        <van-radio name="2" checked-color="#3cafff" @click="sendMsg = '2'">发送</van-radio>
      </van-radio-group>
    </div>

    <div class="submit-box" v-show="hideBtn">
      <button type="button" class="submit" @click="submit">提交</button>
    </div>

    <v-dialog v-model="sendVal" content="请仔细检查您编辑的招聘管理信息，是否确定提交？" cancelText="取消" confirmText="确定"
              @cancel="cancel" @confirm="submitEdit"></v-dialog>

    <backpage @backPage="backPage" class="back-btn"></backpage>
  </div>
</template>

<script>
  import {baseUrl} from "../../../config/env"
  import {PopupPicker, Datetime} from 'vux'
  import dialog from "../../../components/dialog"
  import {setSessionStore, getSessionStore} from "../../../config/util"
  import backpage from "../../../components/backpage"

  export default {
    data() {
      return {
        id: "",
        userSign: "",
        workInfo: {},
        maxNumber: "",
        startTime: "",
        endTime: "",
        status: [],
        statusList: [['招聘中', '待招聘']],
        sendVal: false,
        demand: "",
        salary: "",
        sleep: "",
        time: "",
        know: "",
        remark: "",
        sendMsg: '1',
        age: "",
        docmHeight: document.documentElement.clientHeight,//默认屏幕高度
        showHeight: document.documentElement.clientHeight,//实时屏幕高度
        hideBtn: true,//显示或者隐藏
        sexAge: "",  //性别年龄
        tattooHair: "",  //纹身染发
        medicalFee: "",  //体检
        vision: "",  //视力
        culturalSkills: "",  //文化技能
        nation: "",  //民族
        idCard: "",  //身份证
        postOther: "", //岗位其他要求
        workingPrice: "",  //底薪说明
        hoursPrice: "",  //工价
        overtimeDetails: "", //加班费说明
        subsidyDetails: "",  //补贴说明
        payrollTime: "", //发薪时间
        salaryOther: "", //薪资福利其他说明
        accConditions: "", //住宿
        diet: "", //饮食
        accOther: "",  //食宿条件其他说明
        workSystem: "",  //工作制度
        shiftTime: "", //班次时间
        workOther: "", //工作时间其他说明
        interviewData: "", //面试资料
        interviewOther: "",  //面试其他说明
      }
    },
    mounted() {
      let base64 = require('js-base64').Base64;
      this.userSign = base64.decode(sessionStorage.getItem("success"));
      this.id = this.$route.query.id;

      this.sexAge = this.$route.query.sexAge;
      this.tattooHair = this.$route.query.tattooHair;
      this.medicalFee = this.$route.query.medicalFee;
      this.vision = this.$route.query.vision;
      this.culturalSkills = this.$route.query.culturalSkills;
      this.nation = this.$route.query.nation;
      this.idCard = this.$route.query.idCard;
      this.postOther = this.$route.query.postOther;

      this.workingPrice = this.$route.query.workingPrice;
      this.hoursPrice = this.$route.query.hoursPrice;
      this.overtimeDetails = this.$route.query.overtimeDetails;
      this.subsidyDetails = this.$route.query.subsidyDetails;
      this.payrollTime = this.$route.query.payrollTime;
      this.salaryOther = this.$route.query.salaryOther;

      this.accConditions = this.$route.query.accConditions;
      this.diet = this.$route.query.diet;
      this.accOther = this.$route.query.accOther;

      this.workSystem = this.$route.query.workSystem;
      this.shiftTime = this.$route.query.shiftTime;
      this.workOther = this.$route.query.workOther;

      this.interviewData = this.$route.query.interviewData;
      this.interviewOther = this.$route.query.interviewOther;

      window.onresize = () => {
        return (() => {
          this.showHeight = document.body.clientHeight;
        })()
      };
      this.queryWorkDetail();
    },
    methods: {
      queryWorkDetail() {
        this.$vux.loading.show({
          text: '加载中',
        });

        let time = (new Date()).getTime();
        let url = baseUrl + "/resident/query_work_list?timestamp=" + time;
        let param = {"sgin": this.userSign, "timestamp": time};
        let signature = this.$signature(url, param);

        this.axios.get(baseUrl + "/resident/query_work_list?sgin=" + this.userSign + "&id=" + this.id + "&auth=" + signature + "&timestamp=" + time).then((res) => {
          let resultObj = res.data;
          this.$vux.loading.hide();
          if (resultObj.code == 0 && resultObj.data != null) {
            this.workInfo = resultObj.data[0];
            if (getSessionStore("work")) {//获取临时存储数据
              let work = JSON.parse(getSessionStore("work"));
              this.maxNumber = work.maxNumber;
              this.startTime = work.startTime;
              if (work.endTime) {
                this.endTime = work.endTime;
              }

              if (work.status) {
                this.status.push(work.status);
              }
              this.age = work.age;


            } else {

              if (this.workInfo.interviewTime != null && this.workInfo.interviewTime != "") {
                if (this.workInfo.interviewTime.split('-')[0]) {
                  this.startTime = this.workInfo.interviewTime.split('-')[0];
                }

                if (this.workInfo.interviewTime.split('-')[1]) {
                  this.endTime = this.workInfo.interviewTime.split('-')[1];
                }
              }
              this.status.push(this.workInfo.status == 0 ? '招聘中' : '待招聘');
              this.maxNumber = this.workInfo.maxNumber;//招聘总人数
              this.age = this.workInfo.age;

              this.sexAge = this.workInfo.sexAge;
              this.tattooHair = this.workInfo.tattooHair;
              this.medicalFee = this.workInfo.medicalFee;
              this.vision = this.workInfo.vision;
              this.culturalSkills = this.workInfo.culturalSkills;
              this.nation = this.workInfo.nation;
              this.idCard = this.workInfo.idCard;
              this.postOther = this.workInfo.postOther;

              this.workingPrice = this.workInfo.workingPrice;
              this.hoursPrice = this.workInfo.hoursPrice;
              this.overtimeDetails = this.workInfo.overtimeDetails;
              this.subsidyDetails = this.workInfo.subsidyDetails;
              this.payrollTime = this.workInfo.payrollTime;
              this.salaryOther = this.workInfo.salaryOther;

              this.accConditions = this.workInfo.accConditions;
              this.diet = this.workInfo.diet;
              this.accOther = this.workInfo.accOther;

              this.workSystem = this.workInfo.workSystem;
              this.shiftTime = this.workInfo.shiftTime;
              this.workOther = this.workInfo.workOther;

              this.interviewData = this.workInfo.interviewData;
              this.interviewOther = this.workInfo.interviewOther;
            }
          } else {
            this.$vux.toast.text(resultObj.msg, "middle");
          }
        }).catch((error) => {
          this.$vux.loading.hide();
          this.$vux.toast.text("网络异常", "middle");
        });
      },
      cancel() {//取消
        this.sendVal = false;
      },
      submitEdit() {//确定更新
        this.sendVal = false;
        this.$vux.loading.show({
          text: '加载中',
        });

        let tempTime = "";
        if (this.endTime) {
          tempTime = this.startTime + '-' + this.endTime;
        } else {
          tempTime = this.startTime;
        }

        let data = {
          "id": this.id,
          "maxNumber": this.maxNumber,
          "status": String(this.status) == "招聘中" ? 0 : 1,
          "interviewTime": tempTime,
          "infoStatus": this.sendMsg,
          "age": this.age,
          "sexAge": this.sexAge,
          "tattooHair": this.tattooHair,
          "medicalFee": this.medicalFee,
          "vision": this.vision,
          "culturalSkills": this.culturalSkills,
          "nation": this.nation,
          "idCard": this.idCard,
          "postOther": this.postOther,
          "workingPrice": this.workingPrice,
          "hoursPrice": this.hoursPrice,
          "overtimeDetails": this.overtimeDetails,
          "subsidyDetails": this.subsidyDetails,
          "payrollTime": this.payrollTime,
          "salaryOther": this.salaryOther,
          "accConditions": this.accConditions,
          "diet": this.diet,
          "accOther": this.accOther,
          "workSystem": this.workSystem,
          "shiftTime": this.shiftTime,
          "workOther": this.workOther,
          "interviewData": this.interviewData,
          "interviewOther": this.interviewOther
        };
        let time = (new Date()).getTime();
        let url = baseUrl + "/resident/update_work_details?timestamp=" + time;
        let param = {"sgin": this.userSign, "timestamp": time};
        let signature = this.$signature(url, param);

        this.axios.post(baseUrl + "/resident/update_work_details?sgin=" + this.userSign + "&auth=" + signature + "&timestamp=" + time, data).then((res) => {
          let resultObj = res.data;
          this.$vux.loading.hide();
          if (resultObj.code == 0) {
            if (resultObj.data == 0) {
              this.$vux.toast.text("提交失败，请重试", "middle");
            } else {
              sessionStorage.removeItem("work");
              this.$router.push("/workmanage");
            }
          } else {
            this.$vux.toast.text(resultObj.msg, "middle");
          }
        }).catch(() => {
          this.$vux.loading.hide();
          this.$vux.toast.text("网络异常", "middle");
        });
      },
      demandEdit() {//要求修改
        this.initWork();
        this.$router.push({path: "/workdemand", query: {"id": this.id}});
      },
      initWork() {//存储临时数据
        let obj = {};
        if (this.maxNumber) {
          obj.maxNumber = this.maxNumber;
        }
        obj.startTime = this.startTime;
        if (this.endTime) {
          obj.endTime = this.endTime;
        }
        obj.status = String(this.status);
        if (this.age) {
          obj.age = this.age;
        }
        setSessionStore("work", obj);
      },
      submit() {
        if (this.maxNumber == "" || this.maxNumber == undefined || this.maxNumber == null) {
          this.$vux.toast.text("请输入招聘人数", "middle");
          return;
        }
        if (this.age == "" || this.age == undefined || this.age == null) {
          this.$vux.toast.text("请输入性别年龄限制", "middle");
          return;
        }
        this.sendVal = true;
      },
      backPage() {
        sessionStorage.removeItem("work");
        this.$router.push("/workmanage");
      }
    },
    watch: {
      showHeight() {
        if (this.docmHeight > this.showHeight) {
          this.hideBtn = false;
        } else {
          this.hideBtn = true;
        }
      }
    },
    components: {
      Datetime,
      PopupPicker,
      "v-dialog": dialog,
      backpage
    }
  }
</script>

<style lang="stylus" scoped>
  @import "../../../assets/stylus/mixin.styl"
  .edit-wrap
    wh(100%, 100%)
    background: rgba(242, 242, 242, 1)
    overflow: hidden
    .edit-box
      display: flex
      align-items: center
      wh(100%, 2.667rem)
      background: rgba(255, 255, 255, 1)
      margin-bottom: 0.267rem
      .pic1
        margin-left: 0.32rem
        wh(2.133rem, 2.133rem)
        border: 0.013rem solid rgba(229, 229, 229, 1)
        border-radius(0.16rem)
      .company-box
        display: flex
        flex-direction: column
        margin-left: 0.373rem
        .title1
          size-color(0.453rem, rgba(68, 68, 68, 1))
          font-weight: 500
        .title2
          padding-top 0.227rem
          padding-bottom: 0.413rem
          size-color(0.4rem, rgba(102, 102, 102, 1))
        .title3
          size-color(0.373rem, rgba(153, 153, 153, 1))
          font-weight: 500

    .content-box
      fj(space-between, center)
      wh(100%, 1.28rem)
      border-bottom: 0.027rem solid rgba(242, 242, 242, 1)
      background: #FFF
      &:last-child
        border-bottom: none
      .title4
        margin-left: 0.32rem
        size-color(0.427rem, rgba(51, 51, 51, 1))
      .radio-box
        display: flex
        margin-right: 0.32rem
        .radio-send
          margin-right: 0.4rem
      .edit-con
        display: flex
        align-items: center
        flex: 1
        height: 1.28rem
        .edit
          wh(100%, 0.6rem)
          line-height: 0.6rem
          text-align: right
          padding-right: 0.8rem
          size-color(0.427rem, rgba(102, 102, 102, 1))
      .num
        flex: 1
        text-align: right
        margin-right: 0.253rem
        size-color(0.427rem, rgba(102, 102, 102, 1))
      .num >>> .weui-cell__ft .vux-cell-placeholder
        color: rgba(204, 204, 204, 1)
      .active1
        color: rgba(255, 187, 76, 1)
      .active2
        color: rgba(60, 175, 255, 1)
      .arrow
        margin-right: 0.347rem
        width: 0.28rem
        height: 0.28rem
        border-top: 0.067rem solid rgba(153, 153, 153, 1)
        border-right: 0.067rem solid rgba(153, 153, 153, 1)
        transform: rotate(45deg)
    .gender-box
      display: flex
      wh(100%, 2.56rem)
      border-bottom: 0.027rem solid #f2f2f2
      background: #FFF
      .gender-title
        margin-top: 0.427rem
        margin-left: 0.32rem
        size-color(0.427rem, rgba(51, 51, 51, 1))
      .gender-select-val
        margin-top: 0.36rem
        margin-left: auto
      .gender-select-val >>> .van-checkbox
        margin-bottom: 0.8rem
      .gender-select-val >>> .van-checkbox .van-checkbox__label
        display: flex
        align-items: center
        size-color(0.427rem, rgba(102, 102, 102, 1))
        padding-top: 0.1rem
        padding-bottom: 0.1rem
      .gender-select-area
        margin-left: 0.88rem
        margin-right: 0.267rem
        size-color(0.427rem, #ccc)
      .gender-select-area >>> .vux-cell-primary .vux-popup-picker-value
        size-color(0.427rem, #333)
      .arrow
        margin-right: 0.347rem
        width: 0.28rem
        height: 0.28rem
        border-top: 0.067rem solid rgba(153, 153, 153, 1)
        border-right: 0.067rem solid rgba(153, 153, 153, 1)
        transform: rotate(45deg)
      .man-box
        display: flex
        align-items: center
    .submit-box
      position: absolute
      bottom: 0.64rem
      wh(100%, 1.28rem)
      text-align: center
      .submit
        wh(90.4%, 1.28rem)
        line-height: 1.28rem
        background: rgba(60, 175, 255, 1)
        border-radius(0.16rem)
        size-color(0.453rem, rgba(255, 255, 255, 1))
        font-weight: 500
    .back-btn
      position: fixed
      bottom: 2.667rem
    .pic-in
      margin-right: 0.32rem
      wh(0.32rem, 0.32rem)
</style>
